package com.getpebble.android.util;

import android.text.TextUtils;
import java.util.Locale;

public enum PblLocale {
    GERMAN("de_DE"),
    FRENCH("fr_FR"),
    ENGLISH("en_US"),
    SPANISH("es_ES"),
    DUTCH("nl_NL"),
    CHINESE_SIMPLIFIED("zh_CN"),
    CHINESE_TRADITIONAL("zh_TW");
    
    public static final PblLocale CHINESE_FALLBACK = null;
    public static final PblLocale DEFAULT_FALLBACK = null;
    private final String locale;

    static {
        CHINESE_FALLBACK = CHINESE_SIMPLIFIED;
        DEFAULT_FALLBACK = ENGLISH;
    }

    private PblLocale(String locale) {
        this.locale = locale;
    }

    public static PblLocale from(Locale locale) {
        if (locale == null) {
            return DEFAULT_FALLBACK;
        }
        String languageCode = locale.getLanguage();
        if (TextUtils.isEmpty(languageCode)) {
            return DEFAULT_FALLBACK;
        }
        if (languageCode.equalsIgnoreCase(Locale.ENGLISH.getLanguage())) {
            return ENGLISH;
        }
        if (languageCode.equalsIgnoreCase(Locale.FRENCH.getLanguage())) {
            return FRENCH;
        }
        if (languageCode.equalsIgnoreCase(Locale.GERMAN.getLanguage())) {
            return GERMAN;
        }
        if (languageCode.equalsIgnoreCase("es")) {
            return SPANISH;
        }
        if (languageCode.equalsIgnoreCase("nl")) {
            return DUTCH;
        }
        if (!languageCode.equalsIgnoreCase(Locale.CHINESE.getLanguage())) {
            return DEFAULT_FALLBACK;
        }
        String countryCode = locale.getCountry();
        if (countryCode == null) {
            return CHINESE_FALLBACK;
        }
        if (countryCode.equalsIgnoreCase(Locale.TRADITIONAL_CHINESE.getCountry())) {
            return CHINESE_TRADITIONAL;
        }
        if (countryCode.equalsIgnoreCase(Locale.SIMPLIFIED_CHINESE.getCountry())) {
            return CHINESE_SIMPLIFIED;
        }
        if (countryCode.equalsIgnoreCase("hk")) {
            return CHINESE_TRADITIONAL;
        }
        if (countryCode.toLowerCase().contains("hans")) {
            return CHINESE_SIMPLIFIED;
        }
        if (countryCode.toLowerCase().contains("hant")) {
            return CHINESE_TRADITIONAL;
        }
        return CHINESE_FALLBACK;
    }

    public String getLocale() {
        return this.locale;
    }
}
